<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>React01</title>

</head>

<body>
  <div class="container"></div>
  <!-- react核心库 -->
  <script src="./js/react.development.js"></script>
  <!-- react拓展库 用于支持react操作dom -->
  <script src="./js/react-dom.development.js"></script>
  <script src="./js/babel.min.js"></script>

  <script type="text/babel">
    class Life extends React.Component {
      state = {
        opacity: 1
      }

      /* change = () => {
        setInterval(() => {
          let { opacity } = this.state
          if (opacity > 0) {
            this.setState({ opacity: opacity - 0.1 })
          } else {
            this.setState({ opacity: 1 })
          }
        }, 200)
      } */

      // 组件完成挂载开始调用
      componentDidMount() {
        this.timer = setInterval(() => {
          let { opacity } = this.state
          if (opacity > 0) {
            this.setState({ opacity: opacity - 0.1 })
          } else {
            this.setState({ opacity: 1 })
          }
        }, 200)
      }

      componentWillUnmount() {
        clearInterval(this.timer)
      }

      // 组件初始化渲染和状态变化开始调用
      render() {
        /* setInterval(() => {
          let { opacity } = this.state
          if (opacity > 0) {
            this.setState({ opacity: opacity - 0.1 })
          } else {
            this.setState({ opacity: 1 })
          }
        }, 200) */
        return (
          <div>
            <h3 ref={c => this.h3Ref = c} style={{ opacity: this.state.opacity }}>React学不会怎么办?</h3>
            <button onClick={this.death}>不活了</button>
            {/* <button onClick={this.change}>开始变化</button> */}
          </div>
        )
      }

      death = () => {
        // clearInterval(this.timer)
        // console.log(this.h3Ref.innerText)
        ReactDOM.unmountComponentAtNode(document.querySelector('.container'))
      }
    }

    ReactDOM.render(<Life />, document.querySelector('.container'))
  </script>
</body>

</html>